package com.runvp.common.pay.wechat.face;

import com.alibaba.fastjson.JSONObject;
import com.runvp.common.pay.wechat.SignUtil;
import com.runvp.common.pay.wechat.V3Sign;
import com.runvp.common.utils.http.HutoolHttpClient;
import com.runvp.common.utils.security.Md5Utils;
import com.runvp.common.utils.uuid.IdUtils;
import com.wechat.pay.java.core.util.PemUtil;
import lombok.extern.slf4j.Slf4j;

import java.security.PrivateKey;
import java.util.*;


@Slf4j
public class PUtils {

    public static void main(String[] args) {

        buildReq();
    }

    private static void buildReq(){
        Map<String,Object> param = new HashMap<>();
        param.put("store_id","RL100000000001");
        param.put("store_name","润来商城");
        param.put("device_id","UDIFJUJHEDSSWDWDDQWWDEFGG");
        param.put("rawdata","IH7VF0j/CFvr8xoHDO4l+WzVAaan8Yra55FdlOhcU+iG1vGYNEsoHwVQFfeveGCldXLaN87TCdJDmCpZEisYA+SZcvc06aC+2XD8ktU56YUVd6KemxxXZU+3IS+lB9ZIysjy+jfuUjdItxXkfNumELjjzcyJGoSuMJUlQfxjVP8ibj3NsrKYoJDHpV0KS0PZzzAXssPTnUMfbW/Oed5hAXoX/JV7hrVlt8w2HVtDAQOTuQqhdGrbc9YWHF74E9m59dB3OIji87eYog1IoOdMDnDM+uStfoZJo/VYXGG4eO+e9TlFKwKkCTo+kMETZPs9xqfp6WcFrd+aqQQ7usqoAse8ZxyqS3iPyH4jNxMFnu/ebomi8fJIf6ZmG99kkqWtO+md/VmWYkE8BY33EtkMI0M8ogu3D/fcB7Zc+zPAuLsA+RPGvPLh6Et3nox5/70djJAwWT6JI6LeoezH77cPNHi03NKb47c3xZUgLjt4b9mgCPiG0yADtj+bSJhpNPdcTnLzos0e0xtcOwZxccxmzxR7XmgSGILtmknKV0RhBssBoy0yRfxVNEyYP6ltSfrBKeHWXUWZzdvtvGlINbgbAiLhiriWNk9QhaH/KLUT2P1Rym6Zg0fF02iCf2Gt+UEEA2InCezqxWWliKd013RtqwTd0aJIZno=");
        param.put("appid","wx68f61fad047fe76a");
        param.put("mch_id","1631613404");
        param.put("now", System.currentTimeMillis()/1000+"");
        param.put("version","1");
        param.put("sign_type","MD5");
        String nonceStr = IdUtils.fastSimpleUUID().toUpperCase();
        param.put("nonce_str",nonceStr);

        List<String> keys = new ArrayList<>(param.keySet());
        Collections.sort(keys);

        param.put("sign",md5Sign(param));
//        param.put("sign",HmacSha256Util.getHmacSha256(buildSignStr(param),"wv3dsjdkjs41hj3SDKJLDSXUasconvf3"));


        String url = "https://payapp.weixin.qq.com/face/get_wxpayface_authinfo";
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type","application/json");
        String res = HutoolHttpClient.post(headers,url, JSONObject.toJSONString(param));
        System.out.println(res);
    }



    private static String md5Sign(Map<String, Object> result){
        StringBuilder sb = new StringBuilder();
        sb.append(buildSignStr(result)).append("&").append("key=wv3dsjdkjs41hj3SDKJLDSXUasconvf3");
        System.out.println(sb.toString());
        String sign = Md5Utils.hash(sb.toString()).toUpperCase();
        System.out.println(sign);
        return sign;
    }

    private static String buildSignStr(Map<String,Object> param){
        StringBuilder sb = new StringBuilder();
        for(Map.Entry<String,Object> entry:param.entrySet()){
            sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
        }
        return sb.substring(0,sb.length()-1);
    }
}
